/*
 * Copyright (C) 2012 Trilarion
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.anote;

import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.WindowConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import org.tools.ui.helper.LookAndFeel;

/**
 *
 * @author Trilarion 2012
 */
public class EditorPaneTest extends JDialog {
    private static final Logger LOG = Logger.getLogger(EditorPaneTest.class.getName());

    private static final long serialVersionUID = 1L;

    /**
     * Creates new form EditorPaneTest
     * @param parent
     * @param modal
     */
    public EditorPaneTest(Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        String content = "<html><h1>Heading</h1><b>bold</b><br><a href=\"http://www.google.de\">Link</a></html>";
        editorPane.setText(content);
        textPane.setText(content);
        editorPane.addHyperlinkListener(new HyperlinkListener() {

            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    textArea.setText("clicked on " + e.getURL());
                }
            }

        });
        transferButtonActionPerformed(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new JScrollPane();
        editorPane = new JEditorPane();
        transferButton = new JButton();
        scrollPane2 = new JScrollPane();
        textArea = new JTextArea();
        loadButton = new JButton();
        urlTextField = new JTextField();
        jScrollPane1 = new JScrollPane();
        textPane = new JTextPane();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("EditorPane Test");
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        setLocationByPlatform(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        editorPane.setContentType("text/html");
        editorPane.setEditable(false);
        scrollPane.setViewportView(editorPane);

        transferButton.setText("Update");
        transferButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                transferButtonActionPerformed(evt);
            }
        });

        textArea.setColumns(20);
        textArea.setEditable(false);
        textArea.setRows(5);
        scrollPane2.setViewportView(textArea);

        loadButton.setText("Load");
        loadButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                loadButtonActionPerformed(evt);
            }
        });

        urlTextField.setText("http://www.google.de");

        textPane.setContentType("text/html");
        jScrollPane1.setViewportView(textPane);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                    .addComponent(scrollPane, Alignment.TRAILING)
                    .addComponent(jScrollPane1, Alignment.TRAILING)
                    .addComponent(scrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(loadButton)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(urlTextField, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE))
                            .addComponent(transferButton))
                        .addGap(0, 490, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(loadButton)
                    .addComponent(urlTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(transferButton)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void transferButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_transferButtonActionPerformed
        textArea.setText(editorPane.getText());
    }//GEN-LAST:event_transferButtonActionPerformed

    private void loadButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
        try {
            editorPane.setPage(urlTextField.getText());
        } catch (IOException ex) {
            LOG.log(Level.INFO, null, ex);
        }
    }//GEN-LAST:event_loadButtonActionPerformed

    private void formWindowClosing(WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        LookAndFeel.setSystemLookAndFeel();

        /*
         * Create and display the dialog
         */
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                EditorPaneTest dialog = new EditorPaneTest(new JFrame(), true);
                dialog.addWindowListener(new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JEditorPane editorPane;
    private JScrollPane jScrollPane1;
    private JButton loadButton;
    private JScrollPane scrollPane;
    private JScrollPane scrollPane2;
    private JTextArea textArea;
    private JTextPane textPane;
    private JButton transferButton;
    private JTextField urlTextField;
    // End of variables declaration//GEN-END:variables
}
